
#import "SheetViewController.h"
#import "SheetCell.h"

@interface SheetViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (strong, nonatomic) IBOutlet UIView *bgView;
@property (nonatomic, strong) UITableView *tableView;
@property (strong, nonatomic) IBOutlet NSLayoutConstraint *bgViewH;
@end

@implementation SheetViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    [self initUI];
}

- (void)initUI{
    self.bgView.bgHexColor = @"#FFFFFF";
    self.withCancel = self.withCancel;
}
- (void)setWithCancel:(BOOL)withCancel{
    _withCancel = withCancel;
    self.bgViewH.constant = self.withCancel ? 44 : 60;
    self.bgView.backgroundColor = self.withCancel ? [UIColor clearColor] : [UIColor whiteColor];
    self.tableView.c_height.c_equal(50 * (self.array.count + 1));
    [self.tableView reloadData];
}
- (void)setArray:(NSArray *)array{
    _array = array;
    self.tableView.c_height.c_equal(50 * (self.array.count + 1));
    [self.tableView reloadData];
}
- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.showsHorizontalScrollIndicator = NO;
        _tableView.translatesAutoresizingMaskIntoConstraints = NO;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.scrollEnabled = NO;
        _tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectZero];
        _tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
        [_tableView registerNib:[SheetCell nib] forCellReuseIdentifier:[SheetCell ID]];
        if (@available(iOS 11.0, *)) {
            _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        }else{
            self.automaticallyAdjustsScrollViewInsets = NO;
        }
        [self.view addSubview:_tableView];
        _tableView.c_height.c_equal(150);
        _tableView.c_left.equalTo(self.view.c_left).coffset(0);
        _tableView.c_right.equalTo(self.view.c_right).coffset(0);
        _tableView.c_bottom.equalTo(self.view.c_bottom).coffset(0);
    }
    return _tableView;
}
#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.withCancel ? 2 : 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return section == 0 ? self.array.count : 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    SheetCell *cell = [tableView dequeueReusableCellWithIdentifier:[SheetCell ID]];
    if (indexPath.section == 0 && self.array.count > 0){
        cell.line.hidden = (indexPath.row == self.array.count - 1);
        cell.title.text = LS(self.array[indexPath.row]);
        cell.title.textHexColor = @"#323232";
    }else{
        cell.line.hidden = YES;
        cell.title.text = LS(@"取消");
        cell.title.textHexColor = @"#AAADB3";
    }
    return cell;
}
#pragma mark - UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [self dismissViewControllerAnimated:NO completion:^{
        if (indexPath.section == 0) {
            if (self.block) {
                self.block((int)indexPath.row,self.array[indexPath.row]);
            }
        }
    }];
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return self.withCancel ? 10 : 0;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return  nil;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    if (self.withCancel) {
        UIView *view = [UIView new];
        view.backgroundColor = [UIColor clearColor];
        return view;
    }
    return nil;
}
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0 && indexPath.row == 0){
        CGRect bounds = CGRectMake(0, 0, SCREEN_WIDTH, cell.bounds.size.height);
        UIBezierPath * path = [UIBezierPath bezierPathWithRoundedRect:bounds byRoundingCorners:UIRectCornerTopLeft | UIRectCornerTopRight cornerRadii:CGSizeMake(20, 20)];
        CAShapeLayer *layer = [CAShapeLayer layer];
        layer.frame = bounds;
        layer.path = path.CGPath;
        cell.layer.mask = layer;
    }else{
        cell.layer.mask = nil;
    }
}
@end
